package com.blockmeta.bbs.minemodule.activity;

import android.content.Intent;
import android.net.Uri;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.StatusBarUtils;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterConstant;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.business.UmengEvent;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.businesslibrary.util.DeepLinkUtils;
import com.blockmeta.bbs.businesslibrary.util.DialogUtil;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.R2;
import com.blockmeta.bbs.minemodule.adapter.CoinTaskCenterAdapter;
import com.blockmeta.bbs.minemodule.adapter.GlideImageLoader;
import com.blockmeta.bbs.minemodule.pojo.CoinCenterPOJO;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
@Route(path = RouterPath.TASK_CENTER_ACTIVITY,extras = RouterConstant.EXTRAL_LOGIN)
public class TaskCenterActivity extends BaseActivity implements OnBannerListener {

    @BindView(R2.id.rl_header)
    LinearLayout mRlHeader;
    @BindView(R2.id.rcv_task_center_list)
    RecyclerView mRcvTaskCenterList;
    @BindView(R2.id.tv_coin_count)
    TextView mTvCoinCount;
    //连续签到天数
    @BindView(R2.id.tv_sign_in_day)
    TextView mTvSignInDay;
    @BindView(R2.id.iv_sign_button)
    ImageView mIvSignButton;
    //签到标签 1天 2天……
    @BindView(R2.id.iv_sign_1)
    ImageView mIvSign1;
    @BindView(R2.id.iv_sign_2)
    ImageView mIvSign2;
    @BindView(R2.id.iv_sign_3)
    ImageView mIvSign3;
    @BindView(R2.id.iv_sign_4)
    ImageView mIvSign4;
    @BindView(R2.id.iv_sign_5)
    ImageView mIvSign5;
    @BindView(R2.id.iv_sign_6)
    ImageView mIvSign6;
    @BindView(R2.id.iv_sign_7)
    ImageView mIvSign7;
    //banner
    @BindView(R2.id.banner_img)
    Banner mBanner;

    private List<CoinCenterPOJO.BannerBean> mBannerBeans;

    private ArrayList<CoinCenterPOJO.TasksBean> mCenterPOJOS = new ArrayList<>();
    private CoinTaskCenterAdapter mAdapter;
    //已经签到的天数
    private int mSignedDays;
    @Override
    protected boolean showTitle() {
        return false;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_task_center, null);
    }

    @Override
    public void setStatusBarColor() {
        StatusBarUtils.setDarkMode(this);
        StatusBarUtils.setWindowStatusBarColor(this,R.color.status_trans);
        StatusBarUtils.setWindowFullScreen(this);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mRcvTaskCenterList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new CoinTaskCenterAdapter(this,mCenterPOJOS);
        mRcvTaskCenterList.setAdapter(mAdapter);
        mRcvTaskCenterList.setNestedScrollingEnabled(false);

        mBanner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        mBanner.setImageLoader(new GlideImageLoader());
        mBanner.setOnBannerListener(this);
        mBanner.setDelayTime(5000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getNetData();
    }

    private void getNetData() {
        OkHttpUtils.get()
                .addHeader("token", SharedPreferenceUtil.getString(this, Constant.TOKEN,null))
                .addHeader("uid", AccountHelper.getUid())
                .url(NetConstant.COIN_CENTER)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if(mTvCoinCount==null) return;
                        CoinCenterPOJO centerPOJO = new Gson().fromJson(response,CoinCenterPOJO.class);
                        mTvCoinCount.setText(String.valueOf(centerPOJO.gold));
                        mSignedDays = centerPOJO.mSignBean.signDays;
                        mTvSignInDay.setText(String.format(Locale.CHINESE,"连续签到%d天", mSignedDays));
                        boolean isSigned = centerPOJO.mSignBean.signToday;
                        mIvSignButton.setEnabled(!isSigned);
                        if (isSigned){
                            mIvSignButton.setImageResource(R.drawable.btn_signed_in);
                        }
                        updateSignDay(mSignedDays);
                        mCenterPOJOS.clear();
                        mCenterPOJOS.addAll(centerPOJO.tasks);
                        mAdapter.notifyDataChanged();

                        mBannerBeans = centerPOJO.banner;

                        List<String> bannerUrls= new ArrayList<>();
                        for (CoinCenterPOJO.BannerBean bannerBean : mBannerBeans) {
                            bannerUrls.add(bannerBean.iconUrl);
                        }
                        mBanner.setImages(bannerUrls);
                        mBanner.start();
                    }
                });
    }

    @OnClick({R2.id.btn_left, R2.id.btn_right,R2.id.iv_sign_in_about, R2.id.iv_sign_button, R2.id.iv_sign_7})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_left) {
            //退出当前页面
            finish();
        } else if (i == R.id.btn_right) {
            //跳转领取详情页面
            Intent intent = new Intent(this, CoinGotDetailActivity.class);
            intent.putExtra("title", "领取明细");
            intent.putExtra("url", NetConstant.COIN_GOT_DETAIL);
            startActivity(intent);

            UmengEvent.eventStatistics(this, Constant.U_MINE_task_gotdetail, null);

        } else if (i == R.id.iv_sign_in_about) {
            //连续签到规则
            DialogUtil.showExplainDialog(this, R.layout.dialog_signin_explain);
            UmengEvent.eventStatistics(this, Constant.U_MINE_task_rule, null);

        } else if (i == R.id.iv_sign_button) {
            //签到按钮
            signIn();
            UmengEvent.eventStatistics(this, Constant.U_MINE_task_dailybonus, null);

        } else if (i == R.id.iv_sign_7) {
        }
    }

    /**
     * 签到
     */
    private void signIn() {
        OkHttpUtils.post()
                .addHeader("token", SharedPreferenceUtil.getString(this, Constant.TOKEN,null))
                .addHeader("uid", AccountHelper.getUid())
                .url(NetConstant.TASK_SIGNIN)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if(mUnbinder == null || mTvCoinCount == null){
                            return;
                        }
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int coins = jsonObject.getInt("award");
                            int total=Integer.valueOf(mTvCoinCount.getText().toString())+coins;
                            mTvCoinCount.setText(String.valueOf(total));
                            ToastUtil.showGotCoinToast(TaskCenterActivity.this, coins+"金币");
                            mIvSignButton.setEnabled(false);
                            mIvSignButton.setImageResource(R.drawable.btn_signed_in);
                            updateSignDay(++mSignedDays);
                            mTvSignInDay.setText(String.format(Locale.CHINESE,"连续签到%d天", mSignedDays));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void updateSignDay(int days) {
        switch (days) {
            case 7:
                mIvSign7.setImageResource(R.drawable.icon_gift_open);
            case 6:
                mIvSign6.setImageResource(R.drawable.icon_goldcoin);
            case 5:
                mIvSign5.setImageResource(R.drawable.icon_goldcoin);
            case 4:
                mIvSign4.setImageResource(R.drawable.icon_goldcoin);
            case 3:
                mIvSign3.setImageResource(R.drawable.icon_goldcoin);
            case 2:
                mIvSign2.setImageResource(R.drawable.icon_goldcoin);
            case 1:
                mIvSign1.setImageResource(R.drawable.icon_goldcoin);
                break;
            default:
                break;
        }
    }

    @Override
    public void OnBannerClick(int position) {
        DeepLinkUtils.jumpToTarget(this, Uri.parse(mBannerBeans.get(position).link));
        UmengEvent.eventStatistics(this,Constant.U_MINE_task_shop,null);
    }
}
